package mongo

import (
	"fmt"
	"gitee.com/baizhige/go-server-tempalte/common/config"
	"gitee.com/baizhige/go-server-tempalte/common/response"
	"gitee.com/baizhige/go-server-tempalte/common/router"
	"gitee.com/baizhige/go-server-tempalte/domain/plat_user"
	"gitee.com/baizhige/go-server-tempalte/domain/role"
	"gitee.com/baizhige/message-go"
	"gitee.com/baizhige/mongong-go"
	"gitee.com/baizhige/mongong-go/db"
	"os"
	"time"
)

func Init(config config.MongoConfig) {
	//连接
	err := db.InitDB(config.Uri, config.Database)
	if err != nil {
		fmt.Println("mongo init fail ,", err)
		os.Exit(1)
	}
	//注册插件

	//初始化索引
	mongong.InitIndex(
		response.RespCode{},
		router.Router{},
		router.RequestLog{},
		plat_user.PlatUser{},
		role.Role{},
		role.Permission{},
	)

	// 初始化完成消息
	message.Publisher(response.MongoInitPost{})        //发布初始化成功事件
	message.Close[response.MongoInitPost](time.Second) //该事件其实只可能启动时产生一次，以后再也不会产生了
}
